package com.jh.user.repository;

import com.jh.user.pojo.UserRole;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

@Repository
public interface UserRoleRepository extends
		JpaRepository<UserRole,String>,JpaSpecificationExecutor<UserRole>{

	/*
	 * 权限管理
	 */
	@Query("select userrole from  UserRole userrole where userrole.status='0' ")
	List<UserRole> findAllUserRole();

	@Query("select userrole from  UserRole userrole where userrole.status='0' ")
	Page<Object> findAllUserRole1(Pageable pageable);

	@Query("select userrole from  UserRole userrole where userrole.status='0'and userrole.userId=:userId and userrole.roleId=:roleId ")
	Page<UserRole> findAllUserRole(@Param("userId") long userId,@Param("roleId")long roleId,Pageable pageable);

	@Query("select userrole from  UserRole userrole where userrole.userId=:userId ")
	List<UserRole> findAllUserRoleByuserId(@Param("userId") long userId);

	@Query("select userrole from  UserRole userrole where userrole.status='0'and userrole.userId=:userId ")
	Page<UserRole> findAllUserRoleByuserId(@Param("userId") long userId,Pageable pageable);


	@Query("select userrole from  UserRole userrole where userrole.status='0' and userrole.roleId=:roleId ")
	Page<UserRole> findAllUserRoleByroleId(@Param("roleId")long roleId,Pageable pageable);

	@Query(" from Role role,UserRole userrole,User user where role.id=userrole.roleId and userrole.userId=?1 ")
	Page<Object> UserRolepageALL(Pageable pageable,@Param("phone")String phone);



	@Query("select userrole from  UserRole userrole where  userrole.userId=:userId")
	UserRole UserRolepageByRUid(@Param("userId") long userId);

	@Query(" from Role role,UserRole userrole,User user where role.id=userrole.roleId and userrole.userId=user.id  and user.id=:userId")
	Page<Object> UserRolepageByuserId(@Param("userId")long userId,Pageable pageable);

	@Query("from Role role,UserRole userrole,User user where role.id=userrole.roleId and userrole.userId=user.id  and role.id=:roleId ")
	Page<Object> UserRolepageByroleId(@Param("roleId")long roleId,Pageable pageable);

	@Query("select userrole from UserRole userrole where userrole.userId in (:usersid) and userrole.roleId=:roleid")
	List<UserRole> findUserRoleByRoleid(@Param("usersid") Long[] usersid, @Param("roleid") long roleid);

	@Modifying
	@Query("update UserRole  set status='1' where userId=:userId")
	public void delUserRole(@Param("userId") long userId);


	@Modifying
	@Query("update UserRole set roleId=:roleId where userId=:userId ")
	void updateUserRoleByUserId(@Param("userId") long userId,@Param("roleId") long roleId);


	@Modifying
	@Query("update UserRole set roleId=:roleId,status='0' where userId=:userId ")
	UserRole updateUserRoleByStart(@Param("userId") long userId,@Param("roleId") long roleId);

	@Query("select ur from UserRole ur where ur.userId = :userId")
	UserRole findUserRoleByUserId(@Param("userId") long userId);

	UserRole findUserRolesByUserIdAndStatus(long userid,String status);

	UserRole findUserRoleByUserIdAndRoleIdAndStatus(long userId,long roleId,String status);

	@Modifying
	@Query("update UserRole set roleId=:roleId,brandId=:brandId,certigieruserId=:certigieruserId,status=:status,createTime=:creatTime where userId=:oldUserId and roleId=:oldRoleId ")
	int newUpdate(@Param("oldUserId")long oldUserId,@Param("oldRoleId")long oldRoleId,@Param("roleId")long roleId,@Param("brandId")long brandId,@Param("certigieruserId")long certigieruserId,@Param("status")String status,@Param("creatTime")Date creatTime);

	//UserRole findUserRolesByUserIdAndStatus(long userid,String status);

	//注销用户记录
	@Modifying
	@Query("delete from UserRole ur where ur.userId=:userid")
	void delUserRoleByUserid(@Param("userid") long userid);

	@Query("select r from UserRole r where r.userId = :userId")
    UserRole queryUserRole(@Param("userId") long userId);

	@Query("select r from UserRole r where r.userId = :userId and r.brandId = :brandId")
	UserRole queryByUserIdAndBrandId(@Param("userId") long userId, @Param("brandId") long brandId);
}
